package com.lanou.impl;

import com.alibaba.fastjson.JSON;
import com.lanou.mapper.SysUserMapper;
import com.lanou.pojo.*;
import com.lanou.service.SysUserService;
import com.lanou.utils.JsonResult;
import net.sf.jsqlparser.expression.StringValue;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SysUserServiceImpl
 * @Description: TODO
 * @Author 郅占伟
 * @Date 2023年10月30日
 * @Version: 1.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public String selectSysUserByName(String userName) {
        SysUser sysUser = sysUserMapper.selectSysUserByUserName(userName);

        // 根据名字查询部门
        SysDept sysDept = sysUserMapper.selectSysDeptById(String.valueOf(sysUser.getDeptId()));

        // 根据userId查询sys_user_post表获取post_id
        SysUserPost sysUserPost = sysUserMapper.selectSysUserPost(String.valueOf(sysUser.getUserId()));

        // 根据post_id查询SysPost
        SysPost sysPost = sysUserMapper.selectSysPostById(String.valueOf(sysUserPost.getPostId()));

        List<String> deptInfo = new ArrayList<>();
        deptInfo.add(sysDept.getDeptName()); // 获取华蓝信息
        deptInfo.add(sysPost.getPostName());
        sysUser.setDeptInfo(deptInfo);

        // 根据user_id查询sys_user_role
        List<SysUserRole> sysUserRoles = sysUserMapper.selectSysUserRoleByUserId(String.valueOf(sysUser.getUserId()));

        List<SysRole> roleList = new ArrayList<>();
        for (SysUserRole sysUserRole:sysUserRoles){
            SysRole sysRole = sysUserMapper.selectSysRoleByRoleId(String.valueOf(sysUserRole.getRoleId()));
            roleList.add(sysRole);
        }
        sysUser.setRoles(roleList);

        JsonResult<SysUser> jsonResult = new JsonResult<>();
        jsonResult.setData(sysUser);
        jsonResult.setStatus(200);

        return JSON.toJSONString(jsonResult);

    }
}
